home *** CD-ROM | disk | FTP | other *** search
- Path: news.halcyon.com!usenet
- From: george potts <solvris@halcyon.com>
- Newsgroups: comp.lang.c
- Subject: Re: revised code of calling a function twice from printf
- Date: Tue, 05 Mar 1996 16:46:28 -0800
- Organization: Solveris Inc
- Message-ID: <313CE064.6E6C@halcyon.com>
- References: <4hfs54$k4e@newsbf02.news.aol.com>
- NNTP-Posting-Host: blv-pm14-ip6.halcyon.com
- Mime-Version: 1.0
- Content-Type: text/plain; charset=us-ascii
- Content-Transfer-Encoding: 7bit
- X-Mailer: Mozilla 2.0 (WinNT; I)
-
- Razine wrote:
- >
- > Here is the actual code , modified with everyone's suggestions. However
- > there is still a bug in here somewhere. Can anyone please explain what is
- > wrong with this.
- >
- > It returns
- >
- > [1] NONE [2] NE
- >
- > I would like it to return
- >
- > [1] NONE [2] Asprin
- >
- > Thank you very much for your help. it is greatly appreciated.
- >
- > #include <stdio.h>
- > #include <string.h>
- >
- > char *display_drug_type(int drug_index);
- >
- > int drug_inventory[5]={0,1,0,0,0};
- >
- > int main() {
- >
- > printf("[1] %s [2] %s
- > \n",display_drug_type(0),display_drug_type(1));
- >
- > return 0;
- > }
- >
- > char *display_drug_type(int drug_index) {
- > char drug_type[81]="\0";
- >
- > switch(drug_inventory[drug_index]) {
- > case 0 : strcpy(drug_type,"NONE"); break;
- > case 1 : strcpy(drug_type,"Asprin"); break;
- > default : printf("Error in Display_drug_inventory");
- > }
- > return drug_type;
- > }
-
- you're returning a pointer to a character array that goes out of
- scope after the return. the memory could then be used for
- something else before being displayed. move your drug_type
- array.
-